import axios from 'axios';
import React, {useEffect, useState} from 'react';
import {Table, Button, notification } from 'antd'
const Audit = () => {
const {roleId, region, username} = JSON.parse(localStorage.getItem("token"))
    const [dataSource, setdataSource] = useState([])
    useEffect(() => {
        axios.get(`/news?auditState=1&_expand=category`).then(res=>{
            const list = res.data
            setdataSource(roleId === 1?list:[
                ...list.filter(item=>item.author===username),
                ...list.filter(item=>item.region===region && item.roleId === 3)
            ])
        })
    }, [roleId, region, username]);
    // 通过按钮
    const handleAudit = (item, auditState, publishState) => {
        setdataSource(dataSource.filter(data=>data.id !== item.id))
        axios.patch(`/news/${item.id}`,{
            auditState,
            publishState
        }).then(res=>{
            notification.info({
                message: `审核成功`,
                description:`您可以去审核列表中查看您的新闻`,
                placement:'bottomRight',
              });
        })
    }
    const columns = [
        {
            title: '新闻标题',
            dataIndex: 'title',
            render:(title, item)=>{
                return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
            }
        },
        {
            title: '作者',
            dataIndex: 'author',
        },
        {
            title: '新闻分类',
            dataIndex: 'category',
            render:(category)=>{
                return category.title
            }
        },
        {
            title: '操作',
            render: (item)=>{
                return <div>
                      <Button danger onClick={()=>handleAudit(item,3,0)}>
                            驳回
                        </Button>
                        &nbsp;
                        <Button type='primary' onClick={()=>handleAudit(item,2,1)}>
                            通过
                        </Button>
                      
                </div>
            }
        },
      ];
    return (
        <div>
            <Table dataSource={dataSource} columns={columns} pagination={{
                pageSize:5
            }} rowKey={item=>item.id}/>
        </div>
    );
};

export default Audit;